using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Diagnostics;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Net;

namespace CKS.EBE.WebControls
{
    public class Twitter : XmlBasedControl
    {
        private string _xslTransformFile;

        /// <summary>
        /// is false if not specified
        /// </summary>
        public string TransformXslName
        {
            get { return _xslTransformFile; }
            set { _xslTransformFile = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            Transform.XslName = TransformXslName;
            base.OnInit(e);
        }


        protected string GetTwitterStatuses()
        {
            string twitterStatuses;
            try
            {
                //Make a call to TinyUrl.com passing in the Url to shorten
                WebRequest request = WebRequest.Create("http://twitter.com/statuses/user_timeline.xml?screen_name=garybushey&count=3");
                WebResponse response = request.GetResponse();
                using (StreamReader responseFromMakeTiny = new StreamReader(response.GetResponseStream()))
                {
                    //Read in the tiny Url
                    twitterStatuses = responseFromMakeTiny.ReadToEnd();
                }
                return twitterStatuses;
            }
            catch (Exception ex)
            {
                return string.Format("<Error>{0}</Error>", ex.Message);
            }
        }

        protected override XmlDocument BuildDocument()
        {
            try
            {
                XmlDocument oDoc = new XmlDocument();
                oDoc.LoadXml(GetTwitterStatuses());

                return oDoc;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                return null;
            }
        }
    }

}
